/*=========================================================================
 Copyright (c) 2008, Nokia Corporation and/or its subsidiary(-ies).
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
     * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
     * Neither the name of the Nokia Corporation and/or its subsidiary(-ies)nor the
       names of its contributors may be used to endorse or promote products
       derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================================*/

#ifndef __NCVPYRAMID_H__
#define __NCVPYRAMID_H__

#include <e32base.h>
#include "ncvImage.h"
#include "ncvMatrix.h"


/**
 * \class CImagePyramid
 * \brief Creates a Laplacian or Gaussian image pyramid from a given bitmap.
 *
 */
class CImagePyramid : public CBase
	{
public:
	/**
	 * \enum TImagePyramidType
	 *
	 * Possible values are:
	 *	- EGaussianPyramid
	 *	- ELaplacianPyramid
	 * \note Symbian TClass.
	 */
	IMPORT_C enum TImagePyramidType
		{
		EGaussianPyramid,
		ELaplacianPyramid
		};
	/**
	 * \brief Constructor
	 * \param aImage CNokiaCVImage to construct the pyramid from
	 * \param aLevels How many levels in the pyramid. If the number is too large it will be truncated.
	 * \param aType Which type of pyramid to construct. Default = Gaussian.
	 * \note the function can leave (NewL)
	 */
	IMPORT_C static CImagePyramid* NewL(TInt aLevels, TImagePyramidType aType = EGaussianPyramid);

	/**
	 * \brief Constructor
	 * \param aImage CNokiaCVImage to construct the pyramid from
	 * \param aLevels How many levels in the pyramid. If the number is too large it will be truncated.
	 * \param aType Which type of pyramid to construct. Default = Gaussian.
	 * \note the function can leave (NewLC)
	 */
	IMPORT_C static CImagePyramid* NewLC(TInt aLevels, TImagePyramidType aType = EGaussianPyramid);

	/**
	 * \brief Set base image of the pyramid
	 */
	IMPORT_C void SetImageL(CNokiaCVImage* aImage);	
	
	/**
	 * \brief Destructor
	 */
	IMPORT_C virtual ~CImagePyramid();

	/**
	 * \brief Getter for level count.
	 * \return TInt
	 */
	IMPORT_C TInt LevelCount();
	/**
	 * \brief Getter for an image of certain level.
	 * \param aLevel Which level image to get.
	 * \return CNokiaCVImage* Pointer to the level image
	 */
	IMPORT_C CNokiaCVImage* LevelImage(TInt aLevel);
	/**
	 * \brief Collapse the pyramid.
	 * \return CNokiaCVImage* Pointer to the collapsed pyramid. If pyramid is Gaussian the pointer
	 * points to the original image at level 0 and the ownership remains with the pyramid.
	 * However if the pyramid is Laplacian then the returned pointer points to a new CNokiaCVImage
	 * which contains the collapsed image from all the Laplacian levels. In this case the ownerships
	 * transfers and user must take care of deallocation.
	 * \note the function can leave
	 */
	IMPORT_C void Collapse(CNokiaCVImage* aTarget);
	/**
	 * \brief Getter for type
	 * \return TImagePyramidType
	 */
	IMPORT_C TImagePyramidType Type();
protected:
	void ConstructL();
	CImagePyramid(TInt aLevels, TImagePyramidType aType);
private:
private:
	void CalculateNextGaussianLevelL(CNokiaCVImage* aUpper, CNokiaCVImage* aLower);
	void CalculateLaplacianLevelL(CNokiaCVImage* aUpper, CNokiaCVImage* aLower, CNokiaCVImage* aTarget);
	CNokiaCVImage* CreateNextLevelL(CNokiaCVImage* aImage);
	CNokiaCVImage* CreateCurrentLevelL(CNokiaCVImage* aImage);	
	void DownSampleByHalfL(CNokiaCVImage* aImage, CNokiaCVImage* aTarget);

	CNokiaCVImage* UpSampleByHalfL(CNokiaCVImage* aImage);
	void AddPixel(TUint16& r, TUint16& g, TUint16& b, CPixelAccess* pix );

	TInt				iLevels;
	TImagePyramidType	iType;
	TDisplayMode		iDispMode;
	TSize				iOrigSize;
	TBool				bSetImage;

	// Temporary image to help processing
	CNokiaCVImage*			iTemp1;
	CNokiaCVImage*			iTemp2;
	CNokiaCVImage*			iOrgImage;

	CArrayPtrFlat<CNokiaCVImage>*	iGaussianImages;
	CArrayPtrFlat<CNokiaCVImage>*	iLaplacianImages;
	};

#endif
